<template>
  <div class="add-resident">
    <h2>添加老人</h2>
    <form @submit.prevent="submitForm">
      <div class="form-group">
        <label>姓名：</label>
        <input v-model="form.name" required>
      </div>
      
      <div class="form-group">
        <label>年龄：</label>
        <input type="number" v-model="form.age" required>
      </div>
      
      <div class="form-group">
        <label>性别：</label>
        <select v-model="form.gender" required>
          <option value="男">男</option>
          <option value="女">女</option>
        </select>
      </div>
      
      <div class="form-group">
        <label>健康状况：</label>
        <input v-model="form.healthStatus" required>
      </div>
      
      <div class="form-group">
        <label>房间号：</label>
        <input v-model="form.roomNumber" required>
      </div>
      
      <button type="submit">提交</button>
    </form>
  </div>
</template>

<script>
import axios from 'axios'

export default {
  name: 'AddResident',
  data() {
    return {
      form: {
        name: '',
        age: '',
        gender: '',
        healthStatus: '',
        roomNumber: ''
      }
    }
  },
  methods: {
    async submitForm() {
      try {
        const response = await axios.post('/api/add-resident', this.form)
        if (response.data.success) {
          alert('添加成功')
          this.resetForm()
        } else {
          throw new Error(response.data.message)
        }
      } catch (error) {
        alert(error.response?.data?.message || '添加失败')
      }
    },
    resetForm() {
      this.form = {
        name: '',
        age: '',
        gender: '',
        healthStatus: '',
        roomNumber: ''
      }
    }
  }
}
</script>

<style scoped>
.form-group {
  margin-bottom: 15px;
}

label {
  display: block;
  margin-bottom: 5px;
}

input, select {
  width: 100%;
  padding: 8px;
  border: 1px solid #ddd;
  border-radius: 4px;
}

button {
  padding: 10px 20px;
  background-color: #4CAF50;
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
}
</style> 